/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigscommons.web.common.forms;

import com.googlecode.chigscommons.web.common.ValidationStyleBehavior;
import org.apache.wicket.Component;
import org.apache.wicket.Component.IVisitor;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public class ShinyFormVisitor implements IVisitor<Component>, Serializable {
	private final Set<Component> visited = new HashSet<Component>();

	private final boolean allowRenderOnAjaxRequest;

	public ShinyFormVisitor() {
		this(false);
	}

	public ShinyFormVisitor(boolean allowRenderOnAjaxRequest) {
		super();
		this.allowRenderOnAjaxRequest = allowRenderOnAjaxRequest;
	}

	public Object component(Component c) {
		if (!visited.contains(c)) {
			visited.add(c);
			// * hinter das Eingabeelement setzen
			c.setComponentBorder(new RequiredBorder(allowRenderOnAjaxRequest));
			// Disabled bis man weiß, wie vermieden werden kann, das die Message zusätzlich auch im Feedbackpanel angezeigt wird. Ansosten wäre es cool.			
			// Fehler unter dem Element anzeigen
			// c.add(new ValidationMsgBehavior());
			// * Css des Eingabeelementes um error im Fehlerfall erweitern
			c.add(new ValidationStyleBehavior());
		}
		return IVisitor.CONTINUE_TRAVERSAL;
	}
}
